export const state = {
	main: {},
	share: {},
	user: {},
	recharge: {},
	uEmpty: {},
	images: {},
	orderStatus: '',
	themeColor: '#FAC033', //主题色
	record: {},
	coupon: {},
}
export const getters = {
	initMain: state => state.main,
	initShare: state => state.share,
	initUser: state => state.user,
	initRecharge: state => state.recharge,
	themeColor: state => state.themeColor,
	orderStatus: state => state.orderStatus,
	uEmpty: state => state.uEmpty,
	images: state => state.images,
	record: state => state.record,
}


export const actions = {
	async mp_update({ dispatch, commit }, isSet = false) {
		if (uni.canIUse("getUpdateManager")) {
			const updateManager = uni.getUpdateManager();
			updateManager.onCheckForUpdate(function(res) {
				// 请求完新版本信息的回调
				if (res.hasUpdate) {
					updateManager.onUpdateReady(function() {
						uni.showModal({
							title: "更新提示",
							content: "新版本已经准备好，是否重启应用？",
							success: function(res) {
								if (res.confirm) {
									// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
									updateManager.applyUpdate();
								}
							}
						});
					});
					updateManager.onUpdateFailed(function() {
						// 新的版本下载失败
						uni.showModal({
							title: "更新提示",
							content: "请您删除当前小程序，重新打开"
						});
					});
				} else if (isSet == true) {
					uni.$u.toast('已是最新版本');
				}
			});
		}
	},
	async getAppConfig({ dispatch }, data) {
		const result = await uni.$u.http.post('common/init', {
			coupon: data
		});
		if (result.code === 1) {
			return result.data;
		}
		return false;
	},
	async initApp({ dispatch, commit }, coupon) {
		console.log(coupon);
		const result = await dispatch('getAppConfig', coupon);
		if (result) {
			commit('CONFIG', result);
			dispatch('getLoginByUnion');
		}
	},
}


export const mutations = {
	CONFIG(state, payload) {
		state.coupon = payload.coupon
		Object.keys(payload).forEach(k => {
			state[k] = payload[k];
		});
	},
	setOrderStatus(state, data) {
		state.orderStatus = data;
	},
}